<?php

namespace Labirynt\CmsBundle\Admin;

use Sonata\AdminBundle\Admin\Admin;
use Sonata\AdminBundle\Form\FormMapper;
use Sonata\AdminBundle\Datagrid\DatagridMapper;
use Sonata\AdminBundle\Datagrid\ListMapper;
use Sonata\AdminBundle\Show\ShowMapper;
use Labirynt\CmsBundle\Repository\DrawingRepository;
use Doctrine\ORM\EntityRepository;

class DrawingAdmin extends Admin
{
	protected $datagridValues = array(
		'_page'       => 1,
		'_per_page'   => 25,
		'_sort_order' => 'DESC',
		'_sort_by'    => 'date',
	);

	public function getTemplate($name)
	{
		switch ($name)
		{
			case 'edit':
				return 'LabiryntCmsBundle:Admin\Drawing:edit.html.twig';
				break;
			
			default:
				return parent::getTemplate($name);
				break;
		}
	}

	protected function configureListFields(ListMapper $listMapper)
	{
		$listMapper
			->add('number')
			->add('file', 'string', array('label' => 'Podgląd', 'template' => 'LabiryntCmsBundle:Admin\List:obrazek.html.twig'))
			->add('original', null, array('label' => 'Pobierz oryginał', 'template' => 'LabiryntCmsBundle:Admin\List:drawing_original.html.twig'))
			->add('date', 'date', array('label' => 'Data'))
			->add('_action', 'actions', array('actions' => array('edit' => array(), 'delete' => array())))
		;
	}
	
	protected function configureFormFields(FormMapper $formMapper)
	{
		$formMapper
			->add('number', 'hidden')
			->add('date', 'date', array('format' => 'd-M-y', 'widget' => 'single_text', 'label' => 'Data', 'attr' => array('class' => 'datepicker')))
			->add('file', 'file', array('label' => 'Rysunek', 'required' => $this->getSubject()->getFilename() ? false : true))
			->end()
		;
	}
	
	public function getNewInstance()
	{
		$instance = parent::getNewInstance();
		$instance->setDate(new \DateTime());
		
		$last_number = $this->getConfigurationPool()->getContainer()->get('doctrine')->getRepository($this->getClass())->getHighestNumber();
		
		$instance->setNumber(++$last_number);
		
		return $instance;
	}
	
	public function configureShowFields(ShowMapper $showMapper)
	{
		$showMapper
			->add('image')
			->add('date')
		;
	}
	
	protected function configureDatagridFilters(DatagridMapper $filterMapper)
	{
	}
	
	public function getExportFormats()
	{
		return array();
	}
	
	public function postRemove($object)
	{
		$number = $object->getNumber();
		
		$this->getConfigurationPool()->getContainer()->get('doctrine')->getRepository($this->getClass())->updateNumbersHigherThan($number);
	}
	
	public function prePersist($object)
	{
		$repository = $this->getConfigurationPool()->getContainer()->get('doctrine')->getRepository($this->getClass());
		$higher_objects = $repository->getDrawingsOlderThan($object->getDate());

		if (count($higher_objects) > 0)
		{
			$number = $higher_objects[0]->getNumber();
			$repository->updateNumbersHigherThan($number - 1, 'up');
			
			$object->setNumber($number);
		}

	}
	
	public function postUpdate($object)
	{
		$repository = $this->getConfigurationPool()->getContainer()->get('doctrine')->getRepository($this->getClass());
		$repository->updateNumbers();
	}
}
